package windows;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ultrastarography.Song;
import files.FileLoader;

public class MainWindow {
	private static final Logger log = LoggerFactory.getLogger(MainWindow.class);

	private JFrame frmUltrastardeluxeSong;
	private JTable table;
	private JScrollPane scrollPane;
	private JPanel panel;
	private JTextField valueInputField;
	private JButton btnSaveValues;
	private JButton btnSaveToFiles;

	private List<Song> songList;
	private String pathname;

	/**
	 * Launch the application.
	 */
	public static void main(final String[] args) {

		// try {
		// UIManager.installLookAndFeel("SeaGlass",
		// "com.seaglasslookandfeel.SeaGlassLookAndFeel");
		// UIManager
		// .setLookAndFeel("com.seaglasslookandfeel.SeaGlassLookAndFeel");
		// } catch (ClassNotFoundException e) {
		// e.printStackTrace();
		// } catch (InstantiationException e) {
		// e.printStackTrace();
		// } catch (IllegalAccessException e) {
		// e.printStackTrace();
		// } catch (UnsupportedLookAndFeelException e) {
		// e.printStackTrace();
		// }

		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					if (log.isDebugEnabled()) {
						for (String arg : args) {
							log.debug("Arg: {}", arg);
						}
					}

					MainWindow window = new MainWindow(
							args.length > 0 ? args[0] : "NULL");
					window.frmUltrastardeluxeSong.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow(String pathname) {
		this.pathname = pathname;
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmUltrastardeluxeSong = new JFrame();
		frmUltrastardeluxeSong.setTitle("UltraStar-Deluxe Song Manager");
		frmUltrastardeluxeSong.setBounds(100, 100, 803, 296);
		frmUltrastardeluxeSong.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmUltrastardeluxeSong.getContentPane().setLayout(
				new BorderLayout(0, 0));

		List<File> files = FileLoader
				.getInfoFiles("/mnt/d/Program Files/UltraStar Deluxe/songss");
		// List<File> files = FileLoader.getInfoFiles(pathname);
		songList = new ArrayList<Song>();
		for (File file : files) {
			Song newSong;
			try {
				newSong = new Song(file);
				songList.add(newSong);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		valueInputField = new JTextField();
		valueInputField.setColumns(40);

		table = new JTable(new SongTableModel(songList));
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				valueInputField.setText(table.getValueAt(
						table.getSelectedRow(), table.getSelectedColumn())
						.toString());
			}
		});
		table.setFillsViewportHeight(true);
		table.setRowSelectionAllowed(false);
		table.setCellSelectionEnabled(true);

		scrollPane = new JScrollPane(table);
		frmUltrastardeluxeSong.getContentPane().add(scrollPane,
				BorderLayout.CENTER);

		panel = new JPanel();
		frmUltrastardeluxeSong.getContentPane().add(panel, BorderLayout.NORTH);

		btnSaveValues = new JButton("Zapisz do komórek");
		btnSaveValues.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (int row : table.getSelectedRows()) {
					for (int col : table.getSelectedColumns()) {
						if (!table.getValueAt(row, col).equals(
								valueInputField.getText())) {
							table.setValueAt(valueInputField.getText(), row,
									col);
						}
					}
				}

			}
		});
		FlowLayout fl_panel = new FlowLayout(FlowLayout.CENTER, 5, 5);
		panel.setLayout(fl_panel);
		panel.add(valueInputField);
		panel.add(btnSaveValues);

		btnSaveToFiles = new JButton("Zapisz do plików");
		btnSaveToFiles.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				for (Song song : ((SongTableModel) table.getModel())
						.getSongList()) {
					if (song.getChanged()) {
						song.saveChangesToFile();

						if (log.isDebugEnabled()) {
							log.debug("Saving changes to file '{}'",
									song.toString());
						}
					}
				}
			}
		});
		panel.add(btnSaveToFiles);
	}
}
